package com.iot.dao.impl;

import com.iot.dao.FastDfsDao;
import com.iot.domain.MvFile;

import org.csource.common.MyException;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import java.io.IOException;
import java.util.List;

@Component
public class FastDfsDaoImpl implements FastDfsDao {

    //上传文件到 fastDfs
    @Override
    public List<MvFile> uploadFile(List<MvFile>  files) throws IOException, MyException, MyException {


        //初始化配置文件
        ClientGlobal.initByProperties("conf/fastdfs-client.properties");
        //开启tracker服务
        TrackerClient trackerClient=new TrackerClient();

        //获取链接
        TrackerServer trackerServer =trackerClient.getConnection();

        //
        StorageServer  storageServer =null;

        //创建客户端的链接
        StorageClient1 storageClient1= new StorageClient1(trackerServer,storageServer);


        for (MvFile file:
            files) {
            //创建元数据

            NameValuePair[] list=new NameValuePair[1];
            list[0]=new NameValuePair("fileName", file.getOldFileName());

            //上传文件
            String fileId = storageClient1.upload_file1(file.getPath(), file.getFileType(), list);
            file.setDfsFileId(fileId);
            //上传成功后  见数据写入redis
            //System.out.println(fileId);
        }
        return files;

    }



}
